﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GooJaCharts
{
    class GoogleChartWriter
    {
        private FusionCharts.WebParts.Plots _plots = null;
        private FusionCharts.WebParts.GoogleChartType _chartType = FusionCharts.WebParts.GoogleChartType.ColumnChart;
        public int Columns = 1;
        public string ChartTitle = "";
        public string XTitle = "";
        public string YTitle = "";
        public string Y2Title = "";
        private string _colors = "";
        public int ChartWidth = 600;
        public int ChartHeight = 400;

        public GoogleChartWriter(FusionCharts.WebParts.Plots plots
            , FusionCharts.WebParts.GoogleChartType chartType
            , string Colors)
        {
            _plots = plots;
            _chartType = chartType;
            _colors = Colors;
        }

        /// <summary>
        /// Generate the nice XML output for fusion charts
        /// </summary>
        /// <param name="chartTitle">Title of the chart</param>
        /// <param name="xTitle">Title to give to x</param>
        /// <param name="yTitle">Title to give to y</param>
        /// <param name="groupAction">Action to perform (sum, count, ...)</param>
        /// <param name="colors">List of colors to use (separated by ;)</param>
        public string WriteContent()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(WriteJavascript());
            sb.Append(WriteHtml());
            return sb.ToString();
        }

        private static string Encode(string src)
        {
            string html = System.Web.HttpUtility.HtmlEncode(src.Replace("'", "\\'"));
            return html;
        }
        private string GetGoogleChartTypeName()
        {
            return _chartType.ToString();
        }
        private string WriteJavascript()
        {
            string html = ChartJavascriptTemplate.Html;
            html = html.Replace("@@XTitle@@", Encode(XTitle));
            html = html.Replace("@@YTitle@@", Encode(YTitle));
            html = html.Replace("@@ChartTitle@@", Encode(ChartTitle));
            html = html.Replace("@@ChartType@@", GetGoogleChartTypeName());
            html = html.Replace("@@DataSetTable@@", GetDataSetTable());
            html = html.Replace("@@ChartWidth@@", ChartWidth.ToString());
            html = html.Replace("@@ChartHeight@@", ChartHeight.ToString());
            html += "<!--rows:" + _plots.Count().ToString() + "-->\n";
            return html;
        }

        private string GetDataSetTable()
        {
            StringBuilder sb = new StringBuilder();
            string yheaders = string.Format("'{0}'", Encode(YTitle));
            if (Columns == 2)
            {
                yheaders += string.Format(", '{0}'", Encode(Y2Title));
            }
            sb.AppendFormat(
                "\r\n['{0}', {1}]"
                , Encode(XTitle)
                , yheaders
                );
            for (int i = 0; i < _plots.Count(); i++)
            {
                sb.Append(",");
                List<double> yvalues = _plots.GetY(i);
                string ydata = string.Format("{0}", yvalues[0]);
                if (Columns == 2)
                {
                    ydata += string.Format(", {0}", yvalues[1]);
                }

                sb.AppendFormat(
                    "\r\n['{0}', {1}]"
                    , Encode(_plots.GetX(i))
                    , ydata
                    );
            }
            return sb.ToString();
        }

        private string WriteHtml()
        {
            return "\r\n<div id='chart_div'></div>";
        }
    }
}
